/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package connection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import model.Artist;

/**
 *
 * @author Jaice
 */
public class DaoArtist {

    private String artistId;

    public String getArtistId() {
        return artistId;
    }

    public void setArtistId(String artistId) {
        this.artistId = artistId;
    }

    public ArrayList<Artist> getArtist() throws SQLException {
        ArrayList<Artist> list = new ArrayList<Artist>();
        Artist artist = null;

        Connector connect = new Connector();
        Connection conn = connect.getConn();

        String query = "select "
                + " artist_id, "
                + " aname "
                + " from artist "
                + " order by aname ";

        PreparedStatement ps = conn.prepareStatement(query);

        ResultSet rs = ps.executeQuery();
//if was able to get a row
        while (rs.next()) {
            artist = new Artist(rs.getInt("artist_id"),
                    rs.getString("aname"));

            list.add(artist);


        }


        return list;

    }

    public ArrayList<Artist> retrieveGetArtist() throws SQLException, ClassNotFoundException {
        return this.getArtist();

    }

    public boolean AddArtist(Artist artist) throws SQLException {
        Connector connect = new Connector();
        Connection conn = connect.getConn();

        PreparedStatement ps = conn.prepareStatement("INSERT INTO ROOT.ARTIST "
                + "(ANAME) "
                + "VALUES "
                + "(?)");

        ps.setString(1, artist.getName());


        return ps.executeUpdate() != 0 ? true : false;
    }

    public Artist getArtistById() throws SQLException {
        Artist artist = null;

        Connector connect = new Connector();
        Connection conn = connect.getConn();

        String query = "select "
                + " artist_id, "
                + " aname "
                + " from artist "
                + " where artist_id = ?"
                + " order by aname ";

        PreparedStatement ps = conn.prepareStatement(query);

        ps.setString(1, this.artistId);

        ResultSet rs = ps.executeQuery();
//if was able to get a row
        rs.next();

        artist = new Artist(rs.getInt("artist_id"),
                rs.getString("aname"));





        return artist;
    }

    public boolean updateArtist(Artist artist) throws SQLException {
        Connector connect = new Connector();
        Connection conn = connect.getConn();

        PreparedStatement ps = conn.prepareStatement("UPDATE ROOT.ARTIST "
                + " SET ANAME = ? "
                + " WHERE ARTIST_ID = ?");
      
        
        ps.setInt(2, artist.getArtistId());
        ps.setString(1, artist.getName());

        return ps.executeUpdate() != 0 ? true : false;
    }
}
